import axios from "axios"

// 在需要的位置建一个扩展window的变量
const axiosWindow = window as any
axiosWindow.axiosCancel = []  // 全局定义一个存放取消请求的标识

declare module 'axios' {
  export interface AxiosResponse<T = any> extends Promise<T> { }
}

declare module 'axios' {
  interface AxiosInstance {
    (config: AxiosRequestConfig): Promise<any>
  }
}

export const Service = axios.create({
  timeout: 30000, //延迟时间
  baseURL: process.env.REACT_APP_BASE_API,
  headers: {
    "pc-token": "4a82b23dbbf3b23fd8aa291076e660ec",
    "content-Type": "application/x-www-form-urlencoded",
  },
})

//请求拦截
Service.interceptors.request.use((config) => {
  // 取消接口
  config.cancelToken = new axios.CancelToken(cancel => {
    axiosWindow.axiosCancel.push({
      cancel
    })
  })

  return config
})

//响应拦截
Service.interceptors.response.use(
  (response) => response.data,
  (err) => console.log(err)
)